<template>
  <div class="app-container">
    新建机器人
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>

      <el-form-item label="交易账户" prop="ExchangeId">
        <el-select v-model="ruleForm.ExchangeId" placeholder="交易账户">
          <el-option v-for="item in exchangeList" :key="item.name" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="交易对" prop="Pair">
        <!-- <el-input v-model="ruleForm.Pair" /> -->
        <el-select v-model="ruleForm.Pair">
          <el-option v-for="item in appGlobal.PairList" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="策略" prop="strategy">
        <el-select v-model="ruleForm.strategy" placeholder="请选择策略" value-key="id">
          <el-option v-for="item in strategyList" :key="item.id" :label="item.name" :value="item" />
        </el-select>
        <span v-if="ruleForm.strategy">{{ ruleForm.strategy.remark }},id: {{ ruleForm.strategy.id }}</span>
      </el-form-item>
      <!-- <el-form-item label="策略参数" prop="StrategyArgs">
        <el-input v-model="ruleForm.strategy.StrategyArgs" />
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import sapi from '@/api/sysApi'
import exchangeApi from '@/api/exchange'
export default {
  data() {
    return {
      loading: false,
      robotId: '',
      robotName: '',
      periodList: [
        { loading: false, name: '1m', period: 60, sign: 'OneMinute,' },
        { loading: false, name: '"3m"', period: 3 * 60, sign: 'ThreeMinutes,' },
        { loading: false, name: '"5m"', period: 5 * 60, sign: 'FiveMinutes,' },
        { loading: false, name: '"15m"', period: 15 * 60, sign: 'FifteenMinutes,' },
        { loading: false, name: '"30m"', period: 30 * 60, sign: 'ThirtyMinutes,' },
        { loading: false, name: '"1h"', period: 3600, sign: 'OneHour,' },
        { loading: false, name: '"2h"', period: 2 * 3600, sign: 'TwoHours,' },
        { loading: false, name: '"4h"', period: 4 * 3600, sign: 'FourHours,' },
        { loading: false, name: '"6h"', period: 6 * 3600, sign: 'SixHours,' },
        { loading: false, name: '"8h"', period: 8 * 3600, sign: 'EightHours,' },
        { loading: false, name: '"12h"', period: 12 * 3600, sign: 'TwelveHours,' },
        { loading: false, name: '"1d"', period: 24 * 3600, sign: 'OneDay,' },
        { loading: false, name: '"3d"', period: 3 * 24 * 3600, sign: 'ThreeDays,' },
        { loading: false, name: '"1w"', period: 7 * 24 * 3600, sign: 'OneWeek,' },
        { loading: false, name: '"1M"', period: 30 * 24 * 3600, sign: 'OneMonth' }
      ],
      exchangeList: [],
      strategyList: [],
      ruleForm: { // name, ExchangeId,Pair,strategyId,StrategyArgs
        name: '',
        ExchangeId: '',
        Pair: 'BTCUSDT',
        strategyId: '',
        StrategyArgs: '',
        strategy: null
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        ExchangeId: [
          { required: true, message: '交易账户', trigger: 'change' }
        ],
        Pair: [
          { required: true, message: '请输入交易对', trigger: 'blur' }
        ],
        strategy: [
          { required: true, message: '交易账户', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    exchangeApi.getList().then(p => {
      this.exchangeList = p.data
    })
    sapi.getStrategyList().then(p => {
      this.strategyList = p.data
    })
  },
  mounted() {

  },
  methods: {

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.doAdd()
        } else {
          return false
        }
      })
    },
    doAdd() {
      this.ruleForm.strategyId = this.ruleForm.strategy.id
      sapi.newRobot(this.ruleForm).then(p => {
        this.$alert('添加完毕')
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
</style>

